package cn.tedu.hospitalgroupproject.server.content.service.impl;

import cn.tedu.hospitalgroupproject.server.common.ex.ServiceException;
import cn.tedu.hospitalgroupproject.server.common.pojo.vo.PageData;
import cn.tedu.hospitalgroupproject.server.common.web.ServiceCode;
import cn.tedu.hospitalgroupproject.server.content.dao.persist.repository.IClinicRepository;
import cn.tedu.hospitalgroupproject.server.content.dao.persist.repository.IDoctorRepository;
import cn.tedu.hospitalgroupproject.server.content.pojo.entity.Clinic;
import cn.tedu.hospitalgroupproject.server.content.pojo.param.ClinicAddNewParam;
import cn.tedu.hospitalgroupproject.server.content.pojo.param.ClinicUpdateInfoParam;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.ClinicListVO;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.ClinicStandardVO;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.DepartmentListVO;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.DepartmentStandardVO;
import cn.tedu.hospitalgroupproject.server.content.service.IClinicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.sql.rowset.serial.SerialException;


/**
 * 处理医生数据的业务实现类
 *
 * @author java@tedu.cn
 * @version 1.0
 */
@Service
@Slf4j
public class ClinicServiceImpl implements IClinicService {

    @Value("${hospital-appointment.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;

   @Autowired
   private IClinicRepository clinicRepository;
   @Autowired
   private IDoctorRepository doctorRepository;

    public ClinicServiceImpl() {
        log.info("创建业务对象:ClinicServiceImpl");
    }

    @Override
    public void addNew(ClinicAddNewParam clinicAddNewParam) {
        log.debug("开始处理[新增诊所]的业务:{}",clinicAddNewParam);
        Clinic clinic=new Clinic();
        BeanUtils.copyProperties(clinicAddNewParam,clinic);

        clinicRepository.insert(clinic);
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理[删除诊所]的业务,诊所id:{}",id);
        int count=doctorRepository.countByClinicId(id);
        if(count>0){//当前要删除的诊所中还有医生
            String message = "删除诊所失败，尝试删除的诊所下还存在医生数据！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        //当没有医生时执行删除
        clinicRepository.deleteById(id);

    }

    @Override
    public void updateInfoById(ClinicUpdateInfoParam clinicUpdateInfoParam) {
        log.debug("开始处理[根据Id更新诊所]的业务,clinicUpdateInfoParam:{}",clinicUpdateInfoParam);
        ClinicStandardVO currentVO= clinicRepository.getStandardById(clinicUpdateInfoParam.getId());
        if(currentVO==null){
            String message="更新失败!尝试更新的对应门诊数据不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        //判断更新的数据是否重复

        Clinic clinic=new Clinic();
        BeanUtils.copyProperties(clinicUpdateInfoParam,clinic);
        int rows=clinicRepository.updateById(clinic);
        if(rows!=1){
            String message="更新失败!服务器忙,请稍后再试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
    }



    @Override
    public PageData<ClinicListVO> list(Integer pageNum) {
        log.debug("开始处理【查询门诊列表】业务，页码：{}", pageNum);
        PageData<ClinicListVO> pageData = clinicRepository.list(pageNum, defaultQueryPageSize);
        return pageData;

    }

    @Override
    public PageData<ClinicListVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询门诊列表】业务，页码：{}", pageNum);
        PageData<ClinicListVO> pageData = clinicRepository.list(pageNum, pageSize);
        return pageData;

    }
}
